/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package former;

import Tables.AbstractTable;
import Tables.WhereEnum;
import java.util.List;

/**
 *
 * @author FOE
 */
public abstract class AbstractConditionalFormer extends AbstractFormer
{
    
    public AbstractConditionalFormer() 
    {
    }

    @Override
    public String formQuery(AbstractTable object) throws Exception {
        String localSql="";
        switch(object.whereCondition)
        {
            case noUse: 
            {
                localSql = formQueryHelper(object);
            }
            case where:
            {
                if( object.where!=null && !object.where.trim().equals(""))
                {
                    localSql = formQueryHelper(object) +" where "+object.where;
                }
                 break;
            }
            case wherelList:
            {
                formQuery(object, object.whereList, object.whereValueList);
            }        
        }
        setSql(localSql);
        return getSql();
    }

    

   
    public String formQuery(AbstractTable object , List<String> whereColumns ,List<String> whereValues) throws Exception
    {
         StringBuilder builder = new StringBuilder();
         if(whereColumns==null && whereValues==null ||(whereColumns.isEmpty()&&whereValues.isEmpty()))
         {
             builder.append(formQueryHelper(object));
         }
        else if(whereColumns.size()==whereValues.size())
        {
            builder .append(formQueryHelper(object));
            builder.append(" where ");
            int index=-1;
            int i=0;
            for(String col:whereColumns)
            {
                index = getColumnsList().indexOf(this);
                if(index != -1)
                {
                   if(i!=0)
                   {
                       builder.append(" and ");
                   }
                   builder.append(whereColumns.get(index));
                   builder.append(" = '");
                   builder.append(whereValues.get(index));
                   builder.append("'");
                }
                else
                {
                    throw new Exception("No such column "+col);
                }
            }
        }
        else
        {
            throw new Exception("Not equal size for where");
        }
        setSql(builder.toString());
        return getSql();
    }
    
       
     
}
